<?php

namespace app\controllers;


use app\librarys\BaseController;
use app\models\Activity;
use app\models\Institution;
use app\models\InstitutionUpdateRecord;
use app\models\Manager;
use app\models\Permission;
use yii\data\Pagination;

class ActivityController extends BaseController
{
    /**
     * 特价活动列表
     * @return string
     */
    public function actionBargain()
    {
        $currentManager = Manager::getCurrentManager();

        $institutions = null;
        if (Permission::isSystemAdmin()) {
            $institutions = Institution::getAllEnabledListWithHierarchy($currentManager['institution_code']);
        } else {
            $institutions = Institution::getAllEnabledSelfChildren($currentManager['institution_code']);
        }

        $hasModifiedInstitutionList = InstitutionUpdateRecord::getHasModifiedInstitutionCodeList(4);


        $page = \Yii::$app->request->get('page', 1);//分页起始页默认为1
        $name = \Yii::$app->request->get('name');//活动名称
        $institution_code = \Yii::$app->request->get('institution_code');
        $start_datetime = \Yii::$app->request->get('start_datetime');
        $end_datetime = \Yii::$app->request->get('end_datetime');

        //取得产品总数 -用于分页
        $DataCount = 0;
        $PageSize = 10;

        $dataCount = Activity::getBargainCount($currentManager['id'], $currentManager['institution_code'], $institution_code, $name, $start_datetime, $end_datetime);
        $list = Activity::getBargainList($page, $PageSize, $currentManager['id'], $currentManager['institution_code'], $institution_code, $name, $start_datetime, $end_datetime);

        $pagination = new Pagination([
            'defaultPageSize' => $PageSize,
            'totalCount' => $dataCount,
        ]);

        return $this->render('bargain',
            [
                'pagination' => $pagination,
                'list' => $list,
                'current_user_id' => $currentManager['id'],
                'institution_code' => $currentManager['institution_code'],
                'institutions' => $institutions,
                'name' => $name,
                'institution_code' => $institution_code,
                'start_datetime' => $start_datetime,
                'end_datetime' => $end_datetime,
                'has_modified_institution_list' => $hasModifiedInstitutionList
            ]);
    }

    /**
     * 特价活动新增/修改
     * @return string
     */
    public function actionBargainPartView()
    {
        $this->layout = false;

        $id = \Yii::$app->request->post('id');
        $institution_code = \Yii::$app->request->post('institution_code');

        $institutions = null;
        $currentManager = Manager::getCurrentManager();
        if (Permission::isSystemAdmin()) {
            $institutions = Institution::getAllEnabledListWithHierarchy($currentManager['institution_code']);
        } else {
            $institutions = Institution::getAllEnabledSelfChildren($currentManager['institution_code']);
        }

        $item = Activity::getBargainById($id);

        $goods = null;
        if ($id == '') {
            $goods = Activity::getBargainAllGoods($institution_code);
        } else {
            $institution_code = $item['institution_code'];
            $goods = Activity::getBargainGoodsById($id, $institution_code);
        }

        return $this->render('bargain-part-view',
            [
                'item' => $item,
                'goods' => $goods,
                'to_institutions' => $institutions,
                'institution_code' => $institution_code
            ]);
    }

    /**
     * 保存特价活动
     * @return string
     */
    public function actionBargainSaveHandler()
    {
        $currentManager = Manager::getCurrentManager();

        $request = \Yii::$app->request;
        $id = $request->post('id');
        $institution_code = $request->post('institution_code');
        $name = $request->post('name');
        $description = $request->post('description');
        $start_datetime = $request->post('start_datetime');
        $end_datetime = $request->post('end_datetime');
        $is_enable = $request->post('is_enable');
        $goodslist = $request->post('goodslist');

        $result = Activity::saveBargain($id, $institution_code, $name, $description, $start_datetime, $end_datetime, $is_enable, $goodslist);

        //保存到修改记录
        if (!InstitutionUpdateRecord::existRecord($institution_code, 4)) {
            InstitutionUpdateRecord::addRecord($institution_code, 4);
        }

        if ($result['status'] == 0) {
            return $this->renderJson(null, 0, '执行成功');
        }
        return $this->renderJson(null, 1, $result['message']);
    }

    /**
     * 删除
     * @return string
     */
    public function actionBargainDelHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id');
        $institution_code = $request->post('institution_code');
        $result = Activity::delBargain($id);

        //保存到修改记录
        if (!InstitutionUpdateRecord::existRecord($institution_code, 4)) {
            InstitutionUpdateRecord::addRecord($institution_code, 4);
        }


        if ($result['status'] == 0) {
            return $this->renderJson(null, 0, '执行成功');
        }
        return $this->renderJson(null, 1, $result['message']);
    }

    /**
     * 特价活动详情
     * @return string
     */
    public function actionBargainPartDetail()
    {
        $this->layout = false;

        $id = \Yii::$app->request->post('id');
        $institution_code = \Yii::$app->request->post('institution_code');

        $institution = Institution::getInstitution($institution_code);
        $item = Activity::getBargainById($id);
        $goods = Activity::getBargainDetailGoodsById($id, $institution_code);

        return $this->render('bargain-part-detail',
            [
                'item' => $item,
                'institution_name' => $institution['name'],
                'goods' => $goods
            ]);
    }
}